//
//  QDMyCollectListViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/9.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 我的收藏界面
final class QDMyCollectListViewController: QDTextListViewController {

    override func nothingTitle() -> String? { return "暂无收藏" }
    override func nothingImage() -> UIImage? { return "nothing_collect".namedImage }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        hidesBottomBarWhenPushed = true
        tableView.shouldNotRecognizeSimultaneouslyWithGestureRecognizer = true
        // Do any additional setup after loading the view.
    }
}


extension QDMyCollectListViewController {
    
    /// 可以删除编辑
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    /// 支持删除操作
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        
        guard editingStyle == .delete else { return }
        guard let data = datas[indexPath.row] as? QDTextListModel else { return }
        
        //结束更新
        tableView.endUpdates()
        view.showTextHUD(text: "取消收藏中..", delayHidden: 10)
        
        //进行删除
        articleAction(id: data.identifier, type: .collect) { [weak self](msg) -> (Void) in
            
            guard let strongSelf = self else { return }
            
            strongSelf.datas.remove(data)
            strongSelf.tableView.reloadData()
            
            //如果不存在数据
            strongSelf.showNothingView(strongSelf.datas.count == 0)
        }
    }
    
    
    override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    
    override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "取消收藏"
    }
}
